<?php

/**
 * Tag for widget InlineEditBox
 * 
 * attributes:<br/>
 * htmltag: html tag to use for inlineEditBox e.g. span -> <span dojoType="inlineEditBox">MyText</span>. optional. default: span<br/>
 * onerror: name of a js function with signature function (type, error). If error == "noack", than the server action did not answer with "true"
 * 			as response value.
 */

m2import('phpinchains/psp/Tag.php');

class widget_inlineEditBoxHandler extends phpinchains_psp_Tag
{

    /**
     * Is a body tag (if false, body is ignored)
     */
    var $tagType = PSP_BODY_TAG;
    /**
     * Tag name in form namespace:tagname
     */
    var $tagName   = 'widget:inlineEditBox';
	var $htmltag;
	var $requiredAttributes = array('id'=>'\w+');
	var $url = null;
	var $attributes = array(
		'onsave',
		'onchange',
		'oncancel',
	    'onerror',
		'id',
		'mode',
		'htmltag',
		'action',
		'module',
		'module'
	);
	
    function doStartTag()
    {
    	$this->htmltag   = 'span';
    	if (isset($this->attrs['htmltag'])) {
    		$this->htmltag = $this->attrs['htmltag'];
    	}
    	$this->mode = 'text';
    	if (isset($this->attrs['mode'])) {
    		if ($this->attrs['mode'] != 'text' && $this->attrs['mode'] != 'textarea')
    			$this->mode = 'text';
    		else
    			$this->mode = $this->attrs['mode'];
    	}
    	if(isset($this->attrs['action'])) {
    		$env = env::getInstance();
    		$this->url = new phpinchains_tagsupport_URL($env);
    		$this->url->setAction($this->attrs['action']);
    		if (isset($this->attrs['module']))
    			$this->url->setModule($this->attrs['module']);
    	}
    	$this->out->write('<'.$this->htmltag.' dojoType="inlineEditBox" id="'.$this->attrs['id'].'" mode="'.$this->mode.'">');
    	return PSP_EVAL_BODY_BUFFERED;
    }

    function doEndTag()
    {
    	$js  = 'phpinchains.addOnBeforeMakeWidgets(function () {var '.$this->attrs['id'].' = dojo.widget.byId("'.$this->attrs['id'].'"); if ('.$this->attrs['id'].') '.$this->attrs['id'].'.destroy(); });';
    	$js .= 'phpinchains.addOnAfterUpdate(function () {var '.$this->attrs['id'].' = dojo.widget.byId("'.$this->attrs['id'].'");';
    	if(isset($this->attrs['onsave'])) {
    		$js .= 'dojo.event.connect('.$this->attrs['id'].', "onSave", "'.$this->attrs['onsave'].'");';
    	}
    	if(isset($this->attrs['onchange'])) {
    		$js .= 'dojo.event.connect('.$this->attrs['id'].', "onChange", "'.$this->attrs['onchange'].'");';
    	}
    	if(isset($this->attrs['oncancel'])) {
    		$js .= 'dojo.event.connect('.$this->attrs['id'].', "onCancel", "'.$this->attrs['oncancel'].'");';
    	}
    	if(isset($this->attrs['action'])) {
    		$this->url->addParam('newValue', '"+newValue+"');
    		$this->url->addParam('oldValue', '"+oldValue+"');
    		$this->url->addParam('name', $this->attrs['name']);
    		$js .= 'dojo.event.connect('.$this->attrs['id'].', "onSave", function (newValue, oldValue,name) {
	dojo.io.bind({
		load:  function (type, data, evt) { if (evt.responseText != "true") { dojo.widget.byId("'.$this->attrs['id'].'").setValue(oldValue); '.(($this->attrs['onerror'])?$this->attrs['onerror'].'(type, "noack");':'').'}  },
		url: "'.$this->url->toString().'",
		error: '.(($this->attrs['onerror'])?$this->attrs['onerror']:'function (type, error) {dojo.widget.byId("'.$this->attrs['id'].'").setValue(oldValue);}').'
	});
});';
    	}
    	$js .= '});';
    	$ctx = &phpinchains_ApplicationContext::getInstance();
    	$jsout = &$ctx->getJSOutputWriter();
    	$jsout->write($js."\n");
    	
    	$this->out->write($this->body->getBuffer());
    	// TODO check the js here djConfig.searchIds.push("<id>");
        $this->out->write('</'.$this->htmltag.'><script type="text/javascript">dojo.require("dojo.widget.InlineEditBox");</script>');
        return PSP_SKIP_BODY;
    }

}
?>